
# setwd("../")  Use only for windows. Otherwise delete for mac
# Loading the csv files and converting them into time series objects
# These files come from the Hamilton (1992) paper directly
# getwd()
# setwd("/Documents/")
hamilton_final_price <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_price.csv", quote="\"")
hamilton_final_CornS <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_CornS.csv", quote="\"")
hamilton_final_CornF <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_CornF.csv", quote="\"")
hamilton_final_OatsS <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_OatsS.csv", quote="\"")
hamilton_final_OatsF <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_OatsF.csv", quote="\"")
hamilton_final_RyeS <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_RyeS.csv", quote="\"")
hamilton_final_RyeF <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_RyeF.csv", quote="\"")
hamilton_final_LardS <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_LardS.csv", quote="\"")
hamilton_final_LardF <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_LardF.csv", quote="\"")
hamilton_final_TBill <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_TBill.csv", quote="\"")

# The wheat data come from CBOT prices provided by the Centre for Financial History, Cambridge
hamilton_final_WheatS <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatS.csv", quote="\"")
hamilton_final_WheatF <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatF.csv", quote="\"")

# Below is my attempt to incorporate a cost of carry during periods of high carryover
# Below I assume 2 cents per bushel carry (high end of range)
hamilton_final_WheatFadj <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatFadj.csv", quote="\"")
# Below I assume 1 cent per bushel carry (low end of range)
hamilton_final_WheatFadj2 <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatFadj2.csv", quote="\"")

# below for windows only
# hamilton_final_WheatS <- read.table("C:/Users/rasheed//Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatS.csv", quote="\"")
# hamilton_final_WheatF <- read.table("C:/Users/rasheed//Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatF.csv", quote="\"")
# hamilton_final_WheatFadj <- read.table("C:/Users/rasheed/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatFadj.csv", quote="\"")
# hamilton_final_WheatFadj2 <- read.table("C:/Users/rasheed/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatFadj2.csv", quote="\"")
# hamilton_final_WheatS <- read.table("C:/Users/rasheed/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/CBOTWheatS.csv", quote="\"")
# abovefor windows only

hamilton_final_RyeS <- hamilton_final_RyeS[-58]
hamilton_final_RyeF <- hamilton_final_RyeF[-58]
hamilton_final_SepDummy <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_SepDummy.csv", quote="\"")
hamilton_final_MayDummy <- read.table("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Data/OriginalData/hamilton_final_MayDummy.csv", quote="\"")
# Below I convert the data files into time series objects
# This is not technically necessary.  I have since found better ways to do this.
Sep.ts = ts(data=hamilton_final_SepDummy, frequency = 3, start=c(1921,1), end=c(1939,1)) 
May.ts = ts(data=hamilton_final_MayDummy, frequency = 3, start=c(1921,1), end=c(1939,1)) 
# converting them into time series objects
Price.ts = ts(data=hamilton_final_price, frequency = 3, start=c(1920,2), end=c(1939,2)) 
CornS.ts = ts(data=hamilton_final_CornS, frequency = 3, start=c(1920,2), end=c(1939,2)) 
CornF.ts = ts(data=hamilton_final_CornF, frequency = 3, start=c(1920,2), end=c(1939,2)) 
OatsS.ts = ts(data=hamilton_final_OatsS, frequency = 3, start=c(1920,2), end=c(1939,2)) 
OatsF.ts = ts(data=hamilton_final_OatsF, frequency = 3, start=c(1920,2), end=c(1939,2)) 
RyeS.ts = ts(data=hamilton_final_RyeS, frequency = 3, start=c(1920,2), end=c(1939,2)) 
RyeF.ts = ts(data=hamilton_final_RyeF, frequency = 3, start=c(1920,2), end=c(1939,2)) 
LardS.ts = ts(data=hamilton_final_LardS, frequency = 3, start=c(1920,2), end=c(1939,2)) 
LardF.ts = ts(data=hamilton_final_LardF, frequency = 3, start=c(1920,2), end=c(1939,2)) 
TBill.ts = ts(data=hamilton_final_TBill, frequency = 3, start=c(1920,2), end=c(1939,2)) 
WheatS.ts <- ts(data=hamilton_final_WheatS, frequency = 3, start=c(1929,3), end=c(1932,2)) 
WheatF.ts <- ts(data=hamilton_final_WheatF, frequency = 3, start=c(1929,3), end=c(1932,2)) 
WheatFadj.ts <- ts(data=hamilton_final_WheatFadj, frequency = 3, start=c(1929,3), end=c(1932,2)) 
WheatFadj2.ts <- ts(data=hamilton_final_WheatFadj2, frequency = 3, start=c(1929,3), end=c(1932,2)) 

# Table 1 in Hamilton (1992) compares the Futures - Spot price in percentage terms and relates it to the total price deflation from 1930-1933
# But we only need 1930-1933, and that is on data rows 28 to 37
# Note that the cotton and wheat numbers for Table 1 come from another paper
# Futures - Spot levels calculated as per below 
CornSFDiff <- 100 * round (mean(log(CornF.ts [29:37,]/ CornS.ts [29:37,]) * 3), 3)
OatsSFDiff <- 100 * round (mean (log (OatsF.ts [29:37,]/ OatsS.ts [29:37,]) * 3), 3)
RyeSFDiff <- 100 * round (mean (log(RyeF.ts [29:37,]/ RyeS.ts [29:37,]) * 3), 3)
LardSFDiff <- 100 * round (mean(log(LardF.ts [29:37,]/ LardS.ts [29:37,]) * 3), 3)
WheatSFDiff <- 100 * round (mean(log(WheatF.ts[1:8,] / WheatS.ts [1:8,]) * 3), 3)
WheatSFadjDiff <- 100 * round (mean(log(WheatFadj.ts[1:8,] / WheatS.ts [1:8,]) * 3), 3)
WheatSFadjDiff2 <- 100 * round (mean(log(WheatFadj2.ts[1:8,] / WheatS.ts [1:8,]) * 3), 3)


# The above, according to Hamilton, measures the 'expected' deflation

# Now we do the percentage change in the spot prices of the four commodities over the whole period (same data from 28 to 37)

CornSpotDiff <- 100 * round (log (CornS.ts [38,] / CornS.ts [29,]) / 3, 3)
OatsSpotDiff <- 100 * round (log (OatsS.ts [38,] / OatsS.ts [29,]) / 3, 3)
RyeSpotDiff <- 100 * round (log (RyeS.ts [38,] / RyeS.ts [29,]) / 3, 3)
LardSpotDiff <- 100 * round(log (LardS.ts [38,] / LardS.ts [29,]) / 3, 3)
WheatSpotDiff <- 100 * round(log (WheatS.ts [9,] / WheatS.ts [1,]) / 3, 3)

sink("~/Dropbox/Replication Workshop/Student Papers/Saleuddin/Analysis/R/table1.out")
# Replicate Table 1
"Table 1 - Expected and Actual Inflation Rates for [Four] Commodities During the Great Depression"
Table1 <- matrix(c(CornSFDiff,CornSpotDiff,OatsSFDiff,OatsSpotDiff, RyeSFDiff, RyeSpotDiff, LardSFDiff, LardSpotDiff), ncol=2, byrow=TRUE)
colnames(Table1) <- c("Expected","Actual")
rownames(Table1) <- c("Corn","Oats","Rye", "Lard")
Table1 <- as.table(Table1)
"Table 1 - Expected and Actual Inflation Rates for [Four] Commodities During the Great Depression"
Table1
"Now Wheat using CBOT data - high carry case"
WheatSFDiff 
# WheatSFadjDiff is the futures spot price differential adjusted for the cost of carry in high carryover years
WheatSFadjDiff
WheatSpotDiff
"Now Wheat using CBOT data - high carry case"
WheatSFadjDiff2
WheatSpotDiff
sink()
